/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package l1j.server.server.model.trade;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import l1j.server.server.model.L1Inventory;
import l1j.server.server.model.Instance.L1DollInstance;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.Instance.L1PetInstance;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.utils.collections.Maps;

// Referenced classes of package l1j.server.server.model:
// L1Trade

public class L1Trade {
	private static L1Trade _instance = new L1Trade();

	private L1Trade() {
	}

	private AtomicInteger _tradePairId = new AtomicInteger(0);
	private Map<Integer, L1TradePair> _allTradePairs = Maps.newConcurrentMap();

	public static L1Trade getInstance() {
		return _instance;
	}

	private Map<Integer, L1Inventory> _allTradeInventories = Maps
			.newConcurrentMap();

	public L1Inventory loadTradeInventory(int objid) {
		if (_allTradeInventories.containsKey(objid)) {
			return _allTradeInventories.get(objid);
		}
		L1Inventory inv = new L1Inventory();
		_allTradeInventories.put(objid, inv);
		return inv;
	}

	public L1PcInstance getTradePartner(L1PcInstance pc) {
		return _allTradePairs.get(pc.getTradeID()).getPartner(pc);
	}

	public void newTrade(L1PcInstance pc_1, L1PcInstance pc_2) {
		int id = _tradePairId.incrementAndGet();
		pc_1.setTradeID(id);
		pc_2.setTradeID(id);
		_allTradePairs.put(id, new L1TradePair(pc_1, pc_2));
	}

	public void stopTrade(int pairId) {
		_allTradePairs.remove(pairId).stopTrade();
	}

	public void deal(int pairId) {
		_allTradePairs.remove(pairId).deal();
	}

	/** 確認這個道具可以移動到倉庫 賣掉 丟掉 刪除 等等 */
	public static boolean isItemMovable(L1PcInstance pc, L1ItemInstance item) {
		boolean movable = true;
		if (!item.getItem().isTradable()) {
			movable = false;
			pc.sendPackets(new S_ServerMessage(210, item.getItem().getName())); // \f1%0は捨てたりまたは他人に讓ることができません。
		}
		for (L1NpcInstance petNpc : pc.getPetList().values()) {
			if (petNpc instanceof L1PetInstance) {
				L1PetInstance pet = (L1PetInstance) petNpc;
				if (item.getId() == pet.getItemObjId()) {
					movable = false;
					// \f1%0は捨てたりまたは他人に讓ることができません。
					pc.sendPackets(new S_ServerMessage(210, item.getItem()
							.getName()));
					break;
				}
			}
		}
		for (L1DollInstance doll : pc.getDollList().values()) {
			if (item.getId() == doll.getItemObjId()) {
				movable = false;
				pc.sendPackets(new S_ServerMessage(1181)); // 該当のマジックドールは現在使用中です。
				break;
			}
		}
		return movable;
	}
}
